﻿using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using TreaShares.Backoffice.DataLayer;
using TreaShares.Backoffice.Controlers;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;

[DataObjectAttribute(true)]
public class OrganisationManager
{
    internal static OrganisationBasicInfo GetOrganisationInfo(Guid organisationId, bool isAdministrator)
    {
        using (TreasharesDataContext db = new TreasharesDataContext())
        {
            var userName = HttpContext.Current.User.Identity.Name;
           
            var user = db.aspnet_Users.Where(u => u.UserName == userName).FirstOrDefault();
            OrganisationBasicInfo orgBasicInfo = (from org in db.Clients
                                                  where org.Id == organisationId
                                                  select new OrganisationBasicInfo
                                                  {
                                                      Id = org.Id,
                                                      Name = org.Name,
                                                      Active = org.Active,
                                                      Logo = org.CompanyLogo,
                                                      Culture = org.CultureInfoName,
                                                      UserSettings = new DefaultUserSettings(),
                                                      CurrentUserId = user.UserId
                                                  }).SingleOrDefault();
            if (user.UserDefaultSettings != null)
            {
                orgBasicInfo.CurrentUserId = user.UserId;
                DefaultUserSettings dus = Deserialize<DefaultUserSettings>(user.UserDefaultSettings.ToString());
                orgBasicInfo.UserSettings = dus;
            }
            return orgBasicInfo;
        }
    }

    public static string Serialize<T>(T value)
    {

        if (value == null)
        {
            return null;
        }

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
        settings.Indent = false;
        settings.OmitXmlDeclaration = false;

        using (StringWriter textWriter = new StringWriter())
        {
            using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
            {
                serializer.Serialize(xmlWriter, value);
            }
            return textWriter.ToString();
        }
    }
    public static T Deserialize<T>(string xml)
    {

        if (string.IsNullOrEmpty(xml))
        {
            return default(T);
        }

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        XmlReaderSettings settings = new XmlReaderSettings();
        // No settings need modifying here

        using (StringReader textReader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
            {
                return (T)serializer.Deserialize(xmlReader);
            }
        }
    }
    public class OrganisationBasicInfo
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public bool Active { get; set; }
        public string Logo { get; set; }
        public string Culture { get; set; }
        public Guid CurrentUserId { get; set; }
        public DefaultUserSettings UserSettings { get; set; }
    }

    internal static DataTable GetModeratorsEmail(string orgId)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["StrongerOrgString"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("OrgModeratorsEmailGet", connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@OrganisationId", System.Data.SqlDbType.UniqueIdentifier).Value = new Guid(orgId);
            connection.Open();
            DataTable tbl = new DataTable();
            tbl.Load(cmd.ExecuteReader());
            return tbl;
        }

    }

}